package com.financing.wap.web.controller.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.AdvertDTO;
import com.financing.wap.service.IAdvertService;
import com.financing.wap.vo.AdvertVO;
import com.financing.wap.web.controller.BaseController;

@Controller("advertController")
@RequestMapping("/api/advert")
public class AdvertController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IAdvertService advertServiceImpl;
	
	@RequestMapping("/list")
	@ResponseBody
	public ResultMap queryAdvertList(HttpServletRequest request, AdvertVO advertVO){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("In AdvertController queryAdvertList begin!");
			List<AdvertDTO> list = new ArrayList<AdvertDTO>();
			if(StringUtils.isBlank(advertVO.getS()) && StringUtils.isBlank(advertVO.getM())){
				//参数验证
				result.setStatus(ResultMap.STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setMessage("getAdvertPosition is null!");
				return result;
			}
			//赋值 多行记录
			if(!StringUtils.isBlank(advertVO.getM())){
				//赋值 位置list
				List<String> aList = new ArrayList<String>();
				String[] srtM  = advertVO.getM().split(",");
				for (int i = 0; i < srtM.length; i++) {
					aList.add(srtM[i]);
				}
				advertVO.setAdvertPositionList(aList);
				list = advertServiceImpl.queryAdvert(advertVO);
			}
			//赋值 单行记录
			if(!StringUtils.isBlank(advertVO.getS())){
				String[] srtS  = advertVO.getS().split(",");
				for (int i = 0; i < srtS.length; i++) {
					
					AdvertDTO bean = advertServiceImpl.queryAdvertInfo(srtS[i]);
					
					if(bean!=null){	list.add(bean);}
				}
			}
			result.setAttribute("list", list);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
		}catch(Exception e){
			log.error("AdvertController queryAdvertList error!",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
			logger.info("In AdvertController queryAdvertList end!");
		return result;
	}

}
